<template>
  <div class="component-video-upload">
    <div class="plugin-video">
      <div
        tabindex="0"
        class="el-upload el-upload--text el-upload--picture-card"
        @click.prevent.stop="onVideoboxHandle"
      >
        <video
          v-if="modelValue"
          ref="myVideoRef"
          class="video"
          :src="checkFileUrl(modelValue)"
          controls="controls"
        />
        <el-icon v-else>
          <Plus />
        </el-icon>
      </div>

      <div
        v-if="modelValue"
        class="video-del"
        @click="onDeleteImg"
      >
        <el-icon
          size="24"
          class="del-icon"
        >
          <DeleteFilled />
        </el-icon>
      </div>
    </div>

    <!-- 弹窗, 新增视频 -->
    <video-box
      v-if="videoboxVisible"
      ref="videoboxRef"
      @refresh-pic="onRefreshPic"
    />
  </div>
</template>

<script setup>

const emit = defineEmits(['update:modelValue'])

const props = defineProps({
  modelValue: {
    default: '',
    type: String
  },
  disabled: {
    default: false,
    type: Boolean
  }
})

const videoboxVisible = ref(false)

// 打开视频选择窗
const videoboxRef = ref(null)
const onVideoboxHandle = () => {
  if (props.disabled) {
    return false
  }
  videoboxVisible.value = true
  nextTick(() => {
    videoboxRef.value?.init(1)
  })
}

const onRefreshPic = (imagePath) => {
  emit('update:modelValue', imagePath)
}
const onDeleteImg = () => {
  emit('update:modelValue', '')
}

</script>

<style lang="scss" scoped>
.component-video-upload{
  .plugin-video {
    display: inline-block;
    width: auto;
    vertical-align: bottom;
    position: relative;
    overflow: hidden;
    border-radius: 6px;

    .el-upload {
      display: flex;
      align-items: center;
      justify-content: center;
      width: auto;
      min-width: 120px;
      max-width: 300px;
      height: auto;
      min-height: 120px;
      max-height: 250px;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      .video {
        width: auto;
        max-width: 300px;
        // height: 120px;
        height: auto;
        max-height: 250px;
        display: block;
      }
      .el-upload:hover {
        border-color: #409eff;
      }
    }

    .video-del{
      position: absolute;
      top: -100px;
      right: -100px;
      width: 100px;
      height: 100px;
      background: rgba(255, 0, 0, 0.5);
      border-radius: 50px;
      transition: all .5s cubic-bezier(0.1, 1, 0.5, 1.5);

      .del-icon{
        position: absolute;
        bottom: 25px;
        left: 20px;
        color: #fff;
        cursor: pointer;
      }
    }

    &:hover .video-del{
      top: -40px;
      right: -40px;
    }
  }
}

</style>
